/*
 * @Author: chen
 *  绑定用户
 * @Date: 2018-08-07  
 */
layui.config({
}).use(['zlConfig', 'zlContext', 'form', 'element'], function (exports) {
    var $ = layui.$
        , zlContext = layui.zlContext
        , layer = layui.layer
        , zlConfig = layui.zlConfig
        , statusName = zlConfig.global.result.statusName
        , msgName = zlConfig.global.result.msgName
        , dataName = zlConfig.global.result.dataName
        , ERROR = zlConfig.global.result.errorState
        , OK = zlConfig.global.result.okState
        , form = layui.form
        , element = layui.element
        , layerIndex = undefined;

    form.render();
    layerIndex = layer.msg('数据加载中，请稍候...', { time: false, shade: [0.1, '#393D49'] });
    var userBindAction = {
        reloadVcode: function () {
            //重新加载图形验证码
            var vcodekey = $('.layadmin-user-login-body input[name="vcodekey"]').val();
            var url = layui.cache['contentPath'] + "/captcha.jpg?vcodekey=" + vcodekey + "&type=simple&t=" + Math.floor(Math.random() * 100);
            $("#captchaImg").prop("src", url);
        },
        getSmsCode: function () {
            //短信验证码
            var phone = $("#LAY-user-bind-cellphone").val();
            if (!zlContext.verifyPhone(phone, "请输入正确的手机号")) {
                $("#LAY-user-bind-cellphone").focus();
                return false;
            }
            var vcode = $("#LAY-user-bind-vcode").val();
            if (!vcode) {
                layer.msg("请输入图形校验码");
                $("#LAY-user-bind-vcode").focus();
                return false;
            }

            //校验手机号是否存在
            var hasExisted = false;
            zlContext.asyncGet(layui.cache['contentPath'] + "/core/sms/phone/hasexisted", { 'phone': phone }, function (result) {
                if (OK == result[statusName]) {
                    if (result[dataName] && "1" == result[dataName].hasExisted) {
                        hasExisted = true;
                    }
                }
            }, true)
            //手机未注册
            if (!hasExisted) {
                layer.msg("该手机号未注册，无法绑定用户");
                return false;
            }

            var vcodekey = $('.layadmin-user-login-body input[name="vcodekey"]').val();
            var orgid = $('.layadmin-user-login-body input[name="orgid"]').val();
            var param = {
                'phone': phone,
                'vcode': vcode,
                'vcodekey': vcodekey,
                'orgid': orgid
            }
            zlContext.get(layui.cache['contentPath'] + "/wechart/send/bind/smscode", param, function (result) {
                if (OK == result[statusName]) {
                    zlContext.successMsg(result[msgName]);
                } else {
                    userBindAction.reloadVcode();
                    zlContext.errorMsg(result[msgName]);
                }
            }, true);
            zlContext.setCountdown(60, 'LAY-user-bind-send-smscode', true);
        },
        getUserinfo: function () {
            var params = {
                'state': $('.layadmin-user-login-body input[name="state"]').val(),
                'openid': $('.layadmin-user-login-body input[name="openid"]').val()
            }
            //获取微信用户信息
            zlContext.asyncGet(layui.cache['contentPath'] + "/web/ssologin/wechat/userinfo", params, function (result) {
                if (OK == result[statusName] && result[dataName]) {
                    var userinfo = result[dataName];
                    if (userinfo['headimgurl']) {
                        $('.layui-logo .layui-logo-img').attr('src', userinfo['headimgurl']);
                    }
                    $('#LAY-user-nickname').val(userinfo['nickname']);
                    $('.layadmin-user-login-body input[name="openid"]').val(userinfo['openid']);
                    $('.layadmin-user-login-body input[name="RWID"]').val(userinfo['RWID']);
                    $('.layadmin-user-login-body input[name="unionid"]').val(userinfo['unionid']);
                    $('.layadmin-user-login-body input[name="tokenidname"]').val(userinfo['tokenidname']);
                } else {
                    zlContext.errorMsg(result[msgName]);
                }
            }, false);

        }
    }

    userBindAction.getUserinfo();
    form.render();
    layer.close(layerIndex);

    //点击图形验证码重新加载事件
    $("#captchaImg").click(function () {
        userBindAction.reloadVcode();
    });
    $('#layadmin-user-jump-login').click(function () {
        var orgcode = $('.layadmin-user-login-body input[name="orgcode"]').val() || "";
        window.location.href = layui.cache['contentPath'] + "/" + orgcode;
    });
    //下发短信验证码事件
    $("#LAY-user-bind-send-smscode").click(function () {
        userBindAction.getSmsCode();
    });

    //提交
    form.on('submit(LAY-user-bind-submit)', function (obj) {
        var field = obj.field;
        var params = {
            'RWID': field['RWID'],
            'phone': field['phone'],
            'smscode': field['smscode'],
            'vcodekey': field['vcodekey'],
            'vcode': field['vcode'],
            'orgid': field['orgid'],
            'openid': field['openid']
        };
        layerIndex = layer.msg('正在绑定用户，请稍候...', { time: false, shade: [0.1, '#393D49'] });
        zlContext.post(layui.cache['contentPath'] + "/web/ssologin/wechat/dobind", params, function (result) {
            layer.close(layerIndex);
            if (OK == result[statusName] && result[dataName]) {
                layerIndex = layer.msg('绑定成功，正在登陆，请稍候...', { time: false, shade: [0.1, '#393D49'] });
                var data = result[dataName];
                data['orgid'] = field['orgid'];
                data['orgcode'] = field['orgcode'];
                if (!data['loginUrl']) {
                    data['loginUrl'] = layui.cache['contentPath'] + "/" + field['orgcode'];
                }
                zlContext.post(layui.cache['contentPath'] + "/web/ssologin/wechat/dologin", data, function (ret) {
                    layer.close(layerIndex);
                    if (OK == ret[statusName]) {
                        zlContext.successMsg("登陆成功");
                        var userVo = ret[dataName];
                        var tokenidname = $('.layadmin-user-login-body input[name="tokenidname"]').val();
                        location.href = layui.cache['contentPath'] + '/index/?' + tokenidname + "=" + userVo["tokenId"];
                    } else {
                        zlContext.errorMsg(ret[msgName]);
                    }
                }, false);
            } else {
                zlContext.errorMsg(result[msgName]);
                userBindAction.reloadVcode();
            }
        }, false);
        return false;
    });
});

